﻿using System;
using System.Collections.Generic;
using AutoMapper;
using CodeSaude.Application.Interfaces;
using CodeSaude.Application.ViewModels;
using CodeSaude.Domain.Entities;
using CodeSaude.Domain.Interfaces.Service;

namespace CodeSaude.Application.Services
{
    public class MedicamentoApplicationService : BaseApplicationService,  IMedicamentoApplicationService
    {
        private readonly IMedicamentoService _medicamentoService;

        public MedicamentoApplicationService(IMedicamentoService medicamentoService)
        {
            _medicamentoService = medicamentoService;
        }

        public void Add(MedicamentoViewModel medicamentoViewModel)
        {
            var medicamento = Mapper.Map<MedicamentoViewModel, Medicamento>(medicamentoViewModel);

            BeginTransaction();
            _medicamentoService.AdicionarMedicamento(medicamento);
            Commit();
        }

        public MedicamentoViewModel GetById(int id)
        {
            return Mapper.Map<MedicamentoViewModel>(_medicamentoService.GetById(id));
        }

        public IEnumerable<MedicamentoViewModel> GetAll()
        {
            return Mapper.Map<IEnumerable<MedicamentoViewModel>>(_medicamentoService.GetAll());
        }

        public void Update(MedicamentoViewModel medicamentoViewModel)
        {
            var medicamento = Mapper.Map<Medicamento>(medicamentoViewModel);

            BeginTransaction();
            _medicamentoService.Update(medicamento);
            Commit();
        }

        public void Remove(MedicamentoViewModel medicamentoViewModel)
        {
            var user = _medicamentoService.GetById(medicamentoViewModel.MedicamentoId);
            BeginTransaction();
            _medicamentoService.Remove(user);
            Commit();
        }

        public void Dispose()
        {
            _medicamentoService.Dispose();
        }
    }
}
